package com.onjob.security;

import java.util.HashSet;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
  
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Set<Groups> setgroup = new HashSet<Groups>();
	public Set<Groups> getSetgroup() {
		return setgroup;
	}
	public void setSetgroup(Set<Groups> setgroup) {
		this.setgroup = setgroup;
	}
	private Users user ;
	private UsersDao userdo;
	private GroupsDao groupdo;
	private Groups groups;
	private String queryString;
	private String message;
	private String rePassword;
	public String getRePassword() {
		return rePassword;
	}
	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getQueryString() {
		return queryString;
	}
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	public void setUserdo(UsersDao userdo) {
		this.userdo = userdo;
	}
	public UsersDao getUserdo() {
		return userdo;
	}
	public void setUser(Users user) {
		this.user = user;
	}
	public Users getUser() {
		return user;
	}
	
	public GroupsDao getGroupdo() {
		return groupdo;
	}
	public void setGroupdo(GroupsDao groupdo) {
		this.groupdo = groupdo;
	}
	public Groups getGroups() {
		return groups;
	}
	public void setGroups(Groups groups) {
		this.groups = groups;
	}
	 @Transactional(propagation=Propagation.REQUIRED)
	public String register()throws Exception{
	   this.groups = this.groupdo.findByName("普通用户组");
		//this.groups = this.groupdo.findById(1l);
		this.setgroup.add(groups);
		this.user.setGroupsId(this.setgroup);
		this.userdo.saveOrUpdate(user);
		 System.err.println("您已经成功注册");
		return "success";
	}
	 public void validateRegister(){
		 if(this.user.getUserPassword().trim().length()==0){
			 this.addFieldError("user.userPassword", "密码不能为空！");
		 }
		 if(!this.rePassword.equals(user.getUserPassword())){
			 this.addFieldError("rePassword", "两次输入的密码不一致！");
		 }
	 }
	 public void validateUpdateUserInfodo(){
		 if(this.user.getUserPassword().trim().length()==0){
			 this.addFieldError("user.userPassword", "密码不能为空！");
		 }
		 if(!this.rePassword.equals(user.getUserPassword())){
			 this.addFieldError("rePassword", "两次输入的密码不一致！");
		 }
	 }
	 public String  checkUserName(){
		 
		 if(this.userdo.findByUserName(queryString)!=null){
				this.setMessage("用户名已经存在，请您更换用户名注册"); 
			}
			else{
				this.setMessage("用户名可用！"); 
				
			}
		 return SUCCESS;
	 }
	 public String checkEmail()throws Exception{
			System.err.println(this.queryString);
			if(this.userdo.findByEmail(queryString)!=null){
				this.setMessage("邮箱已经存在，请您更换邮箱注册"); 
			}
			else{
				this.setMessage("邮箱可以使用！"); 
				
			}
			return "success";
		}
	 public String updateUserInfo()throws Exception{
	    	
	    	Long uid =((Users)ServletActionContext.getRequest().getSession().getAttribute("user")).getId();
	    	this.user = this.userdo.findById(uid);
	    	return "success";
	    }
	    public String updateUserInfodo()throws Exception{
	    	Long uid =((Users)ServletActionContext.getRequest().getSession().getAttribute("user")).getId();
	        this.user.setGroupsId(this.userdo.findById(uid).getGroupsId());
	    	this.userdo.saveOrUpdate(this.user);
	    	return "success";
	    }
}
